<?php

namespace App\Console\Commands;

use App\Models\AgentConfig;
use App\Services\AdaPayService;
use App\Services\ChargeContext;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;

class TestAdaPayCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:ada-pay {event}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'TestAdaPay';

    protected $chargeContext;

    /**
     * Create a new command instance.
     */
    public function __construct(ChargeContext $chargeContext)
    {
        parent::__construct();

        $this->chargeContext = $chargeContext;
    }

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        $event = $this->argument('event');

        switch ($event) {
            case 'checkout':
                $this->checkout();

                break;

            case 'refund':
                $this->refund();

                break;

            case 'query':
                $this->query();

                break;

            case 'create_corp':
                $this->createCorp();

                break;

            case 'query_corp':
                $this->queryCorp();

                break;

            case 'settle':
                $this->settleAccount();

                break;
        }

        return CommandAlias::SUCCESS;
    }

    private function checkout()
    {
        $this->chargeContext->initInstance(3);

        $res = $this->chargeContext->getUnionPayLink(
            //'CK_' . date('YmdHis') . rand(100000, 999999),
            'S202209091749014095',
            18.8,
            '收银台测试',
            'user_001',
            '',
            'https://port.banquan520.cn/agent',
            Carbon::now()->addMinutes(30)
        );

        dump($res);
    }

    private function refund()
    {
        $config = AgentConfig::query()
            ->where('agent_id', 3)
            ->pluck('config_value', 'config_key')
        ;
        $adaPayService = new AdaPayService($config);

        dump($adaPayService->refund('002112022102710414810431536136288591872', 'CK_' . date('YmdHis') . rand(100000, 999999), 0.02));
    }

    private function query()
    {
        $this->chargeContext->initInstance(3);
        dump($this->chargeContext->queryOrder('CK_20221028150932994579'));
    }

    private function createCorp()
    {
        $config = AgentConfig::query()
            ->where('agent_id', 3)
            ->pluck('config_value', 'config_key')
        ;
        $adaPayService = new AdaPayService($config);
        dump($adaPayService->corpMemberCreat());
    }

    private function queryCorp()
    {
        $adaPayService = new AdaPayService();
        dump($adaPayService->corpMemberQuery());
    }

    private function settleAccount()
    {
        $adaPayService = new AdaPayService();
        dump($adaPayService->settleAccount('vrleo_business_test_202210261533255172'));
    }
}
